use sea_orm::entity::prelude::*;
use salvo::prelude::*;
use serde::{Deserialize, Serialize};
use chrono::NaiveDateTime;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "user_role")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i64,
    pub user_id: String,
    pub role_id: String,
    pub tenant_id: Option<i64>,
    pub create_time: NaiveDateTime,
    pub create_by: String,
    pub update_time: NaiveDateTime,
    pub update_by: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::role::Entity",
        from = "Column::RoleId",
        to = "super::role::Column::Id"
    )]
    Role,
}

impl Related<super::role::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::Role.def()
    }
}

impl ActiveModelBehavior for ActiveModel {}

#[derive(Serialize, Deserialize, Extractible, Debug)]
#[salvo(extract(default_source(from = "body", parse = "json")))]
pub struct UserRole {
    pub id: i64,
    pub user_id: String,
    pub role_id: String,
    pub tenant_id: Option<i64>,
    pub create_time: NaiveDateTime,
    pub create_by: String,
    pub update_time: NaiveDateTime,
    pub update_by: String,
}
